<?php

require_once './library/Zend/Db/Table/Abstract.php';

//require_once './library/Table.php';

class TableConfigNetwork extends Zend_Db_Table
{

    protected $_name;
    protected $_primary;
    protected $_db;
    protected $_registery;
    private $_select;
    protected $_assName;

    protected function _setup()
    {
        $this->_name = 'confignetwork';
        $this->_assName = 'ass_usernetwork';
        $this->_primary = 'IdNetwork';
        $this->_registry = Zend_Registry::getInstance();
        $this->_db = $this->_registry->get('adapter');
        parent::_setup();
    }

    public function getConfigNetwork($pIdUser)
    {
        $this->_select = $this->_db->select();
        $this->_select->from(array('net' => $this->_name),
                array('net.NameNetwork', 'net.LoginNetwork', 'net.PassNetwork'));
        $this->_select->join(array('ass' => $this->_assName), 'ass.IdNetwork = net.IdNetwork', array());
        $this->_select->where('ass.IdUser = ?', $pIdUser);
        $this->_select = $this->_db->query($this->_select);
        $result = $this->_select->fetchAll();
        if (isset($result[0]))
            return $result;
        return false;
    }

    public function getConfigNetworkName($name)
    {
        $this->_select = $this->_db->select();
        $this->_select->from($this->_name);
        $this->_select->where('NameNetwork = ?', $name);
        $this->_select = $this->_db->query($this->_select);
        $result = $this->_select->fetchAll();
        if (isset($result[0]))
            return $result;
        return false;
    }

    public function getConfigNetworkId($idNetwork)
    {
        $this->_select = $this->_db->select();
        $this->_select->from($this->_assName);
        $this->_select->where('IdNetwork = ?', $idNetwork);
        $this->_select = $this->_db->query($this->_select);
        $result = $this->_select->fetchAll();
        if (isset($result[0]))
            return $result;
        return false;
    }

    public function updateConfigNetwork($pName, $pLogin, $pPwd)
    {
        $val = array('LoginNetwork' => $pLogin,
            'PassNetwork' => $pPwd);
        $where = $this->_db->quoteInto('namenetwork = ?', $pName);
        $this->_db->update($this->_name, $val, $where);
    }

    public function addConfigNetwork($pIdUser, $pName, $pLogin, $pPwd)
    {
        $val = array('NameNetwork' => $pName,
            'LoginNetwork' => $pLogin,
            'PassNetwork' => $pPwd);
        $this->_db->insert($this->_name, $val);
        $idNetwork = $this->_db->lastInsertId($this->_name);

        $assVal = array('IdNetwork' => $idNetwork,
            'IdUser' => $pIdUser);
        $this->_db->insert($this->_assName, $assVal);
        return true;
    }

    public function deleteConfigNetwork($pIdUser, $pIdNetwork)
    {
        $this->delete($this->_db->quoteInto('IdNetwork = ?', $pIdNetwork));
        $this->_name = $this->_assName;
        $c1 = $this->_db->quoteInto('IdNetwork = ?', $pIdNetwork);
        $c2 = $this->_db->quoteInto('IdUser = ?', $pIdUser);
        $this->delete($c1, $c2);
        $this->_name = 'confignetwork';
    }

}